﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TTTTBHYT.BUS;
using TTTTBHYT.VO;
using C1.Win.C1FlexGrid;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using System.Data.OleDb;

namespace TTTTBHYT
{
    public partial class frm29BHYT : frmParent
    {
        public MAU29VO Mau29VO;
        private Mau29BUS Mau29BUS;
        private Mau29DetailBUS Mau29DetailBUS;
        private Module.ClickButtonStatus flagButton = Module.ClickButtonStatus.Ngung;//co phan biet dang o trang thai ghi hay ngung

        public frm29BHYT()
        {
            InitializeComponent();
            Mau29BUS = new Mau29BUS();
            Mau29DetailBUS = new Mau29DetailBUS();
            docfiledbf();
        }
        private void frm29BHYT_KeyDown(object sender, KeyEventArgs e)
        {
            Module.SendKey(this, e);
        }
        private void frm29BHYT_Load(object sender, EventArgs e)
        {
            FormatFlex();
            if (flagButton == Module.ClickButtonStatus.Ngung)
            {
                txtKCB.Enabled = false;
            }
        }
        private void docfiledbf()
        {
            string FileName = System.IO.Directory.GetCurrentDirectory().ToString() + "\\dmicd.dbf";
            string Path = FileName.Substring(0);
            int Separator = Path.LastIndexOf("\\");
            string DataSource = FileName.Substring(0, Separator) + "\\";

            string File = FileName.Substring(Separator);
                
            int Separator2 = File.LastIndexOf("\\");
            string DBF = File.Remove(Separator2, 1);

            int Separator3 = DBF.LastIndexOf(".");
            string DBF_Extension = DBF.Substring(Separator3);
            string DBF_FileName = DBF.Remove(Separator3, 4);
            
            //define the connections to the .dbf file
            OleDbConnection conn = new OleDbConnection(@"Provider=vfpoledb;Data Source=" + DataSource + ";Collating Sequence=machine;");

            OleDbCommand command = new OleDbCommand("select ma_icd,rtrim(tenicd) as tenicd from " + DBF_FileName + " order by ma_icd", conn);
            //open the connection and read in all the data from .dbf file into a datatable
            conn.Open();
            DataTable dt = new DataTable();
            dt.Load(command.ExecuteReader());
            cboICD.ClearItems();
            cboICD.AllowSort = true;
            cboICD.DataSource = dt;
            cboICD.Splits[0].DisplayColumns[0].Width = 50;
            cboICD.Splits[0].DisplayColumns[1].Width = 400; 
            conn.Close();  //close connection to the .dbf file
            
        }
        private void FormatFlex()
        {
            int index = 1;
            C1.Win.C1FlexGrid.CellStyle rs = this.fa.Styles.Add("rowcolor");
            rs.ForeColor = Color.Blue;
            rs.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
            fa.Rows[index].Style = rs;
            fa.Rows[index].AllowEditing = false;
            fa.Rows[index]["STT"] = "I";
            fa.Rows[index]["diengiai"] = "Dịch vụ y tế cơ bản";
            index = index + 1;
            fa.Rows[index]["STT"] = "1";
            fa.Rows[index]["diengiai"] = "Xét nghiệm";
            index = index + 1;
            fa.Rows[index]["STT"] = "2";
            fa.Rows[index]["diengiai"] = "Chuẩn đoán HA, TDCN";
            index = index + 1;
            fa.Rows[index]["STT"] = "3";
            fa.Rows[index]["diengiai"] = "Thuốc, dịch truyền";
            index = index + 1;
            fa.Rows[index]["STT"] = "4";
            fa.Rows[index]["diengiai"] = "Máu";
            index = index + 1;
            fa.Rows[index]["STT"] = "5";
            fa.Rows[index]["diengiai"] = "Thủ thuật, phẫu thuật";
            index = index + 1;
            fa.Rows[index]["STT"] = "6";
            fa.Rows[index]["diengiai"] = "Vật tư y tế tiêu hao";
            index = index + 1;
            fa.Rows[index]["STT"] = "7";
            fa.Rows[index]["diengiai"] = "Tiền khám bệnh/ngày giường";
            index = index + 1;
            fa.Rows[index]["STT"] = "8";
            fa.Rows[index]["diengiai"] = "Khác";
            index = index + 1;
            fa.Rows[index]["STT"] = "II";
            fa.Rows[index]["diengiai"] = "Dịch vụ kỹ thuật cao";
            fa.Rows[index].Style = rs;
            fa.Rows[index].AllowEditing = false;
            index = index + 1;
            fa.Rows[index]["STT"] = "1";
            fa.Rows[index]["diengiai"] = "Phẫu thuật, thủ thuật";
            index = index + 1;
            fa.Rows[index]["STT"] = "2";
            fa.Rows[index]["diengiai"] = "Vật tư y tế thay thế";
            index = index + 1;
            fa.Rows[index]["STT"] = "3";
            fa.Rows[index]["diengiai"] = "Cận lâm sàng";
            index = index + 1;
            fa.Rows[index]["STT"] = "III";
            fa.Rows[index]["diengiai"] = "Thuốc K, chống thải ghép \r\n ngoài danh mục";
            fa.Rows[index].HeightDisplay = 30;
            fa.Rows[index].Height = 40;
            index = index + 1;
            C1.Win.C1FlexGrid.CellStyle rs1 = this.fa.Styles.Add("rowcolor1");
            rs1.ForeColor = Color.Red;
            rs1.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
            fa.Rows[index]["STT"] = "IV";
            fa.Rows[index]["diengiai"] = "Tổng cộng I + II + III";
            fa.Rows[index].Style = rs1;
            fa.Rows[index].AllowEditing = false;

             

        }
        private void txtKCB_Leave(object sender, EventArgs e)
        {
            if (txtKCB.Text != "")
            {
                GetKCB();
                if (txtMaBV.Text != "")
                {
                    txtTenBV.Text = Mau29BUS.GetTenBV(txtMaBV.Text);
                }
                else
                {
                    txtMaDT.Text = txtKCB.Text.Substring(0, 2);
                }
            }
        }
        private void GetKCB()
        {
            MAU29VO Mau29VO;
            Mau29VO = Mau29BUS.GetMau29BySoKCBFromBHXH(txtKCB.Text);
            if (Mau29VO.HoTen != null)
            {
                txtMaDT.Text = Mau29VO.MaDT;
                txtHoten.Text = Mau29VO.HoTen.Trim();
                txtGioitinh.Text = Mau29VO.GioiTinh.ToString();
                txtDiachi.Text = Mau29VO.DiaChi;
                txtNgaySinh.Value = Convert.ToDateTime(Mau29VO.NgaySinh);
                txtMaBV.Text = Mau29VO.MaBV;
                txtTungay.Value = Convert.ToDateTime(Mau29VO.TuNgay);
                txtDenngay.Value = Convert.ToDateTime(Mau29VO.DenNgay);
                txtTenBV.Text = Mau29VO.TenBV.Trim();
                txtMaBVDT.Focus();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            switch (flagButton)
            {
                case Module.ClickButtonStatus.Ngung:
                    flagButton = Module.ClickButtonStatus.Them;
                    Mau29VO = new MAU29VO();
                    ClearControl();
                    txtKCB.Enabled = true;
                    break;
                case Module.ClickButtonStatus.Them:
                    if (CheckBeforeSave() == false) { return; }
                    SetValueToMau29();
                    Mau29VO = Mau29BUS.InsertRecord(Mau29VO);
                    if (Mau29VO == null)
                    {
                        MessageBox.Show("Kh«ng ghi ®­îc d÷ liÖu", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("L­u d÷ liÖu thµnh c«ng", "Th«ng b¸o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    }
                    if (Mau29VO.KetLuan == (int)Module.Ketluan.Nhaptheongay) //nhap theo ngay
                    {
                        List<MAU29DETAILVO> lstMau29Detail = SetValueToMau29Detail();
                        foreach (MAU29DETAILVO mau29detailvo in lstMau29Detail)
                        {
                            Mau29DetailBUS.InsertRecord(mau29detailvo);
                        }
                    }
                    // ClearControl();
                    flagButton = Module.ClickButtonStatus.Sua;
                    txtKCB.Enabled = false;
                    break;

                case Module.ClickButtonStatus.Sua:
                    if (CheckBeforeSave() == false) { return; }

                    if (Mau29VO.KetLuan == (int)Module.Ketluan.Nhaptheongay) //nhap theo ngay
                    {
                        Mau29DetailBUS.DeleteRecord(Mau29VO.KeySL);
                        List<MAU29DETAILVO> lstMau29Detail = SetValueToMau29Detail();
                        foreach (MAU29DETAILVO mau29detailvo in lstMau29Detail)
                        {
                            Mau29DetailBUS.InsertRecord(mau29detailvo);
                        }
                    }
                    SetValueToMau29();
                    if (!Mau29BUS.UpdateRecord(Mau29VO))
                    {
                        MessageBox.Show("Kh«ng ghi ®­îc d÷ liÖu", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("CËp nhËp thµnh c«ng", "th«ng b¸o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    }
                    //flagButton = Module.ClickButtonStatus.Sua ;

                    break;
            }
            StatusButton();
        }
        private void StatusButton()
        {
            if (flagButton == Module.ClickButtonStatus.Them || flagButton == Module.ClickButtonStatus.Sua)
            {
                btnThem.Text = "&Ghi";
                btnSua.Text = "&Ngưng";
                btnXoa.Enabled = false;
                btnThoat.Enabled = false;
                txtKCB.Focus();

            }
            else
            {
                btnThem.Text = "&Thêm";
                btnSua.Text = "&Sửa";
                btnXoa.Enabled = true;
                btnThoat.Enabled = true;
                txtKCB.Enabled = false;

            }
        }
        private void ClearControl()
        {

            txtKCB.Text = "";
            txtMaDT.Text = "";
            txtHoten.Text = "";
            txtNgaySinh.Value = DateTime.Now;
            txtGioitinh.Text = "";
            txtTungay.Value = "01/01/" + DateTime.Now.Year;
            txtDenngay.Value = "31/01/" + DateTime.Now.Year;
            txtDiachi.Text = "";
            txtMaBV.Text = "";
            txtTenBV.Text = "";
            txtMaBVDT.Text = "";
            txtTenBVDT.Text = "";
            txtTuNgayDT.Value = DateTime.Now;
            txtDenNgayDT.Value = DateTime.Now;
            txtNgay.Value = DateTime.Now;
            txtSoNgay.Text = "0";
            txtNgayTT.Text = "0";
            txtSLchungtu.Text = "0";
            cboLoaiDieuTri.SelectedIndex = -1;
            cboHangBV.SelectedIndex = -1;
            cboNhapVien.SelectedIndex = -1;
            cboGiayTo.SelectedIndex = -1;
            cboBHYT.SelectedIndex = -1;
            cboTuyen.SelectedIndex = -1;
            cboKetLuan.SelectedIndex = -1;
            cboICD.SelectedValue = "";
            txtChanDoan.Text = "";
            cboTinhtrang.SelectedIndex = 0;
            txtThanhToan.Value = 0;
            txtBHXHTT.Value = 0;
            txtNBChiTra.Value = 0;
            txtNgoaidinhxuat.Value = 0;
            lblTien.Text = "Bằng chữ: ";
            if (DateTime.Now.Month >= 1 && DateTime.Now.Month <= 3)
            {
                txtQuy.Text = "1" + DateTime.Now.Year;
            }
            else if (DateTime.Now.Month >= 4 && DateTime.Now.Month <= 6)
            {
                txtQuy.Text = "2" + DateTime.Now.Year;
            }
            else if (DateTime.Now.Month >= 7 && DateTime.Now.Month <= 9)
            {
                txtQuy.Text = "3" + DateTime.Now.Year;
            }
            else if (DateTime.Now.Month >= 10 && DateTime.Now.Month <= 12)
            {
                txtQuy.Text = "4" + DateTime.Now.Year;
            }

            CellRange rg = fa.GetCellRange(1, 2, fa.Rows.Count - 1, fa.Cols.Count - 1);
            fa.Clear(C1.Win.C1FlexGrid.ClearFlags.Content, rg);
            for (int i = 2; i < fa.Rows.Count; i++)
            {
                fa.Rows[i]["lydo"] = "";
            }
            rg = fa1.GetCellRange(1, 1, fa1.Rows.Count - 1, fa1.Cols.Count - 1);
            fa1.Clear(C1.Win.C1FlexGrid.ClearFlags.Content, rg);
            for (int i = 2; i < fa1.Rows.Count; i++)
            {
                fa1.Rows[i]["lydo"] = "";
            }

        }
        private Boolean CheckBeforeSave()
        {

            if (txtKCB.Text == "")
            {
                MessageBox.Show("NhËp sè KCB.");
                return false;
            }
            if (txtMaDT.Text == "")
            {
                MessageBox.Show("NhËp m· ®èi t­îng.");
                return false;
            }
            if (txtQuy.Text == "")
            {
                MessageBox.Show("NhËp quý.");
                return false;
            }
            if (cboTinhtrang.SelectedIndex < 0)
            {
                MessageBox.Show("NhËp t×nh tr¹ng thanh to¸n.");
                return false;
            }
            if (cboTinhtrang.SelectedIndex == 1 && flagButton == Module.ClickButtonStatus.Sua && Mau29VO.ThanhToan == 1)
            {
                MessageBox.Show("Kh«ng cËp nhËp th«ng tin khi chøng tõ ®· thanh to¸n.");
                return false;
            }
            if (cboLoaiDieuTri.SelectedIndex < 0)
            {
                MessageBox.Show("NhËp lo¹i h×nh ®iÒu trÞ.");
                return false;
            }
            if (cboHangBV.SelectedIndex < 0)
            {
                MessageBox.Show("NhËp h¹ng bÖnh viÖn.");
                return false;
            }
            if (cboTuyen.SelectedIndex < 0)
            {
                MessageBox.Show("NhËp tuyÕt ®iÒu trÞ.");
                return false;
            }
            if (cboKetLuan.SelectedIndex < 0)
            {
                MessageBox.Show("NhËp kÕt luËn.");
                return false;
            }
            return true;
        }
        public void SetValueToFa1()
        {
            List<MAU29DETAILVO> lstMau29Detail = Mau29DetailBUS.GetListMau29Detail(Mau29VO.KeySL);
            int i = 1;
            fa1.Rows.Count = 1;
            foreach (MAU29DETAILVO mau29detailvo in lstMau29Detail)
            {
                fa1.Rows.Count = fa1.Rows.Count + 1;
                fa1.Rows[i]["diengiai"] = mau29detailvo.Ngay;
                fa1.Rows[i]["chiphi"] = mau29detailvo.TienChiPhi;
                fa1.Rows[i]["chiphiKCB"] = mau29detailvo.TienKCB;
                fa1.Rows[i]["lydo"] = mau29detailvo.LyDo;
                fa1.Rows[i]["Ketluan"] = mau29detailvo.KetLuan;
                fa1.Rows[i]["chenhlech"] =
                  Convert.ToInt32(fa1.Rows[i]["chiphi"]) -
                   Convert.ToInt32(fa1.Rows[i]["chiphiKCB"]);
                i++;
            }
            fa1.Rows.Count = fa1.Rows.Count + 1;
            for (i = 1; i < fa1.Rows.Count - 2; i++)
            {
                fa1.Rows[fa1.Rows.Count - 1]["chiphi"] = Convert.ToInt32(fa1.Rows[fa1.Rows.Count - 1]["chiphi"]) + Convert.ToInt32(fa1.Rows[i]["chiphi"]);
                fa1.Rows[fa1.Rows.Count - 1]["chiphiKCB"] = Convert.ToInt32(fa1.Rows[fa1.Rows.Count - 1]["chiphiKCB"]) + Convert.ToInt32(fa1.Rows[i]["chiphiKCB"]);
                fa1.Rows[fa1.Rows.Count - 1]["chenhlech"] = Convert.ToInt32(fa1.Rows[fa1.Rows.Count - 1]["chenhlech"]) + Convert.ToInt32(fa1.Rows[i]["chenhlech"]);

            }
            //txtThanhToan.Value = fa1.Rows[fa1.Rows.Count - 1]["chiphiKCB"];
            //txtBHXHTT.Value = fa1.Rows[fa1.Rows.Count - 1]["chiphiKCB"];
            //txtNBChiTra.Value = 0;
            //lblTien.Text = "Bằng Chữ: " + Module.ChangeToCamel(Module.So_chu(Convert.ToDouble(txtBHXHTT.Value)).Trim()); 
        }
        private List<MAU29DETAILVO> SetValueToMau29Detail()
        {
            List<MAU29DETAILVO> lstMau29Detail = new List<MAU29DETAILVO>();
            MAU29DETAILVO Mau29DetailVO;
            for (int i = 1; i < fa1.Rows.Count - 2; i++)
            {
                if (fa1.Rows[i]["diengiai"] != null)
                {
                    Mau29DetailVO = new MAU29DETAILVO();
                    Mau29DetailVO.KeyGH = Mau29VO.KeySL;
                    Mau29DetailVO.Ngay = fa1.Rows[i]["diengiai"].ToString();
                    Mau29DetailVO.TienChiPhi = Convert.ToInt32(fa1.Rows[i]["chiphi"]);
                    Mau29DetailVO.TienKCB = Convert.ToInt32(fa1.Rows[i]["chiphiKCB"]);
                    Mau29DetailVO.LyDo = Convert.ToString(fa1.Rows[i]["Lydo"]);
                    Mau29DetailVO.KetLuan = Convert.ToString(fa1.Rows[i]["Ketluan"]);
                    lstMau29Detail.Add(Mau29DetailVO);
                }

            }
            return lstMau29Detail;
        }
        private void SetValueToMau29()
        {

            Mau29VO.SoKCB = txtKCB.Text;
            Mau29VO.MaDT = txtMaDT.Text;
            Mau29VO.HoTen = txtHoten.Text;
            Mau29VO.NgaySinh = txtNgaySinh.Text.ToString().Substring(6, 4) + "/" + txtNgaySinh.Text.ToString().Substring(3, 2) + "/" + txtNgaySinh.Text.ToString().Substring(0, 2);
            Mau29VO.GioiTinh = Convert.ToInt32(txtGioitinh.Text);
            Mau29VO.TuNgay = txtTungay.Text.ToString().Substring(6, 4) + "/" + txtTungay.Text.ToString().Substring(3, 2) + "/" + txtTungay.Text.ToString().Substring(0, 2);
            Mau29VO.DenNgay = txtDenngay.Text.ToString().Substring(6, 4) + "/" + txtDenngay.Text.ToString().Substring(3, 2) + "/" + txtDenngay.Text.ToString().Substring(0, 2);
            Mau29VO.DiaChi = txtDiachi.Text.Trim();
            if (txtMaBV.Text != "")
            {
                Mau29VO.MaTinh = txtMaBV.Text.Substring(0, 2);
                Mau29VO.MaTinhDT = txtMaBV.Text.Substring(0, 2);
            }
            else
            {
                Mau29VO.MaTinh = "";
                Mau29VO.MaTinhDT = "";
            }
            Mau29VO.MaBV = txtMaBV.Text;
            Mau29VO.TenBV = txtTenBV.Text;
            Mau29VO.MaBVDT = txtMaBVDT.Text;
            
            Mau29VO.TenBVDT = txtTenBVDT.Text;
            Mau29VO.TuNgayDT = txtTuNgayDT.Text.ToString().Substring(6, 4) + "/" + txtTuNgayDT.Text.ToString().Substring(3, 2) + "/" + txtTuNgayDT.Text.ToString().Substring(0, 2);
            Mau29VO.DenNgayDT = txtDenNgayDT.Text.ToString().Substring(6, 4) + "/" + txtDenNgayDT.Text.ToString().Substring(3, 2) + "/" + txtDenNgayDT.Text.ToString().Substring(0, 2);
            Mau29VO.SoNgay = Convert.ToInt32(txtSoNgay.Text);
            Mau29VO.SoNgayTT = Convert.ToInt32(txtNgayTT.Text);
            Mau29VO.LoaiDT = cboLoaiDieuTri.SelectedIndex;
            Mau29VO.HangBV = cboHangBV.SelectedIndex;
            if (cboICD.SelectedValue != null)
            {
                Mau29VO.MaICD = cboICD.SelectedValue.ToString();
            }
            else
            {
                Mau29VO.MaICD = "";
            }
            Mau29VO.ChuanDoan = txtChanDoan.Text;
            Mau29VO.Quy = txtQuy.Text;
            Mau29VO.NgayCT = txtNgay.Text.ToString().Substring(6, 4) + "/" + txtNgay.Text.ToString().Substring(3, 2) + "/" + txtNgay.Text.ToString().Substring(0, 2);
            if (txtNtt.Value != "")
            {
                Mau29VO.NgayTT = txtNtt.Text.ToString().Substring(6, 4) + "/" + txtNtt.Text.ToString().Substring(3, 2) + "/" + txtNtt.Text.ToString().Substring(0, 2);
            }
            else
            {
                Mau29VO.NgayTT = "";
            }
            Mau29VO.SoChungTu = Convert.ToInt32(txtSLchungtu.Text);
            Mau29VO.ThanhToan = cboTinhtrang.SelectedIndex;
            Mau29VO.TTNV = cboNhapVien.SelectedIndex;
            Mau29VO.LoaiGT = cboGiayTo.SelectedIndex;
            Mau29VO.TheBHYT = cboBHYT.SelectedIndex;
            Mau29VO.TuyenKham = cboTuyen.SelectedIndex;
            Mau29VO.KetLuan = cboKetLuan.SelectedIndex;
            if (txtThanhToan.Text != "")
            {
                Mau29VO.TienThanhToan = Convert.ToInt32(txtThanhToan.Value);
            }
            if (txtBHXHTT.Text != "")
            {
                Mau29VO.BhxhThanhToan = Convert.ToInt32(txtBHXHTT.Value);
            }
            Mau29VO.NbThanhToan = Convert.ToInt32(txtNBChiTra.Value);
            Mau29VO.BhxhNDX = Convert.ToInt32(txtNgoaidinhxuat.Value);
          
           
            int i = 2;
            Mau29VO.XetNghiemLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            Mau29VO.HaLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            Mau29VO.ThuocLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            Mau29VO.MauLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            Mau29VO.ThuthuatLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            Mau29VO.VatTuLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            Mau29VO.TienKhamLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            Mau29VO.KhacLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            i++;
            Mau29VO.PhauThuatCaoLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            Mau29VO.VatTuCaoLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            Mau29VO.LamSangCaoLyDo = fa.Rows[i]["lydo"].ToString();
            i++;
            Mau29VO.ThuocKLyDo = fa.Rows[i]["lydo"].ToString();

            i = 2;
            Mau29VO.XetNghiem = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            Mau29VO.Ha = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            Mau29VO.Thuoc = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            Mau29VO.Mau = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            Mau29VO.Thuthuat = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            Mau29VO.VatTu = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            Mau29VO.TienKham = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            Mau29VO.Khac = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            i++;
            Mau29VO.PhauThuatCao = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            Mau29VO.VatTuCao = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            Mau29VO.LamSangCao = Convert.ToInt32(fa.Rows[i]["chiphi"]);
            i++;
            Mau29VO.ThuocK = Convert.ToInt32(fa.Rows[i]["chiphi"]);

            i = 2;
            Mau29VO.XetNghiemKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            Mau29VO.HaKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            Mau29VO.ThuocKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            Mau29VO.MauKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            Mau29VO.ThuthuatKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            Mau29VO.VatTuKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            Mau29VO.TienKhamKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            Mau29VO.KhacKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            i++;
            Mau29VO.PhauThuatCaoKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            Mau29VO.VatTuCaoKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            Mau29VO.LamSangCaoKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
            i++;
            Mau29VO.ThuocKKCB = Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);

        }
        public void SetValueToControl()
        {

            txtKCB.Text = Mau29VO.SoKCB;
            txtMaDT.Text = Mau29VO.MaDT;
            txtHoten.Text = Mau29VO.HoTen;
            txtNgaySinh.Value = Mau29VO.NgaySinh;
            txtGioitinh.Text = Mau29VO.GioiTinh.ToString();
            txtTungay.Value = Mau29VO.TuNgay;
            txtDenngay.Value = Mau29VO.DenNgay;
            txtDiachi.Text = Mau29VO.DiaChi;
            txtMaBV.Text = Mau29VO.MaBV;
            txtTenBV.Text = Mau29VO.TenBV;
            txtMaBVDT.Text = Mau29VO.MaBVDT;
            txtTenBVDT.Text = Mau29VO.TenBVDT;
            txtTuNgayDT.Value = Mau29VO.TuNgayDT;
            txtDenNgayDT.Value = Mau29VO.DenNgayDT;
            txtSoNgay.Text = Mau29VO.SoNgay.ToString();
            txtNgayTT.Text = Mau29VO.SoNgayTT.ToString();
            cboLoaiDieuTri.SelectedIndex = Mau29VO.LoaiDT;
            cboHangBV.SelectedIndex = Mau29VO.HangBV;
            cboICD.SelectedText  = Mau29VO.MaICD;
            txtChanDoan.Text = Mau29VO.ChuanDoan;
            txtQuy.Text = Mau29VO.Quy;
            txtNgay.Value = Mau29VO.NgayCT;
            txtNtt.Value = Mau29VO.NgayTT; 
            txtSLchungtu.Text = Mau29VO.SoChungTu.ToString();
            cboTinhtrang.SelectedIndex = Mau29VO.ThanhToan;

            cboNhapVien.SelectedIndex = Mau29VO.TTNV;
            cboGiayTo.SelectedIndex = Mau29VO.LoaiGT;
            cboBHYT.SelectedIndex = Mau29VO.TheBHYT;
            cboTuyen.SelectedIndex = Mau29VO.TuyenKham;
            cboKetLuan.SelectedIndex = Mau29VO.KetLuan;
            txtThanhToan.Value = Mau29VO.TienThanhToan;
            txtBHXHTT.Value = Mau29VO.BhxhThanhToan;
            txtNBChiTra.Value = Mau29VO.NbThanhToan;
            txtNgoaidinhxuat.Value = Mau29VO.BhxhNDX;
           
            int i = 2;
            fa.Rows[i]["lydo"] = Mau29VO.XetNghiemLyDo;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.HaLyDo;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.ThuocLyDo;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.MauLyDo;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.ThuthuatLyDo;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.VatTuLyDo;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.TienKhamLyDo;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.KhacLyDo;
            i++;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.PhauThuatCaoLyDo;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.VatTuCaoLyDo;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.LamSangCaoLyDo;
            i++;
            fa.Rows[i]["lydo"] = Mau29VO.ThuocKLyDo;
            i = 2;
            fa.Rows[i]["chiphi"] = Mau29VO.XetNghiem;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.Ha;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.Thuoc;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.Mau;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.Thuthuat;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.VatTu;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.TienKham;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.Khac;
            i++;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.PhauThuatCao;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.VatTuCao;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.LamSangCao;
            i++;
            fa.Rows[i]["chiphi"] = Mau29VO.ThuocK;

            i = 2;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.XetNghiemKCB;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.HaKCB;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.ThuocKCB;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.MauKCB;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.ThuthuatKCB;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.VatTuKCB;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.TienKhamKCB;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.KhacKCB;
            i++;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.PhauThuatCaoKCB;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.VatTuCaoKCB;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.LamSangCaoKCB;
            i++;
            fa.Rows[i]["chiphiKCB"] = Mau29VO.ThuocKKCB;
            //chenh lech
            i = 2;
            fa.Rows[i]["chenhlech"] = Mau29VO.XetNghiem - Mau29VO.XetNghiemKCB;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.Ha - Mau29VO.HaKCB;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.Thuoc - Mau29VO.ThuocKCB;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.Mau - Mau29VO.MauKCB;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.Thuthuat - Mau29VO.ThuthuatKCB;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.VatTu - Mau29VO.VatTuKCB;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.TienKham - Mau29VO.TienKhamKCB;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.Khac  - Mau29VO.KhacKCB ;
            i++;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.PhauThuatCao - Mau29VO.PhauThuatCaoKCB;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.VatTuCao - Mau29VO.VatTuCaoKCB;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.LamSangCao - Mau29VO.LamSangCaoKCB;
            i++;
            fa.Rows[i]["chenhlech"] = Mau29VO.ThuocK - Mau29VO.ThuocKKCB;

            TongDVCB();
            TongDVKTC();

            fa.Rows[15]["chiphi"] = Convert.ToInt32(fa.Rows[10]["chiphi"]) + Convert.ToInt32(fa.Rows[1]["chiphi"]) + Convert.ToInt32(fa.Rows[14]["chiphi"]);
            fa.Rows[15]["chiphiKCB"] = Convert.ToInt32(fa.Rows[10]["chiphiKCB"]) + Convert.ToInt32(fa.Rows[1]["chiphiKCB"]) + Convert.ToInt32(fa.Rows[14]["chiphiKCB"]);
            fa.Rows[15]["chenhlech"] = Convert.ToInt32(fa.Rows[10]["chenhlech"]) + Convert.ToInt32(fa.Rows[1]["chenhlech"]) + Convert.ToInt32(fa.Rows[14]["chenhlech"]);
            lblTien.Text = "Bằng Chữ: " + Module.ChangeToCamel(Module.So_chu(Convert.ToDouble(txtBHXHTT.Value))).Trim();
        }
        public void btnSua_Click(object sender, EventArgs e)
        {
            switch (flagButton)
            {
                case Module.ClickButtonStatus.Ngung:

                    flagButton = Module.ClickButtonStatus.Sua;
                    break;
                case Module.ClickButtonStatus.Sua:

                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;
                case Module.ClickButtonStatus.Them:

                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;

            }
            StatusButton();
        }

        private void txtMaBVDT_Leave(object sender, EventArgs e)
        {
            if (txtMaBVDT.Text.Trim().Length == 5)
            {
                txtTenBVDT.Text = Mau29BUS.GetTenBV(txtMaBVDT.Text);
                //SendKeys.Send("{TAB}");
            }
        }

        public void fa_AfterEdit(object sender, RowColEventArgs e)
        {
            if (fa.Selection.TopRow >= 2)
            {
                fa.Rows[fa.Selection.TopRow]["chenhlech"] =
                  Convert.ToInt32(fa.Rows[fa.Selection.TopRow]["chiphi"]) -
                   Convert.ToInt32(fa.Rows[fa.Selection.TopRow]["chiphiKCB"]);
                if (fa.Selection.TopRow <= 9)
                {
                    TongDVCB();
                }
                if (fa.Selection.TopRow >= 11 && fa.Selection.TopRow <= 13)
                {
                    TongDVKTC();
                }
                fa.Rows[15]["chiphi"] = Convert.ToInt32(fa.Rows[10]["chiphi"]) + Convert.ToInt32(fa.Rows[1]["chiphi"]) + Convert.ToInt32(fa.Rows[14]["chiphi"]);
                fa.Rows[15]["chiphiKCB"] = Convert.ToInt32(fa.Rows[10]["chiphiKCB"]) + Convert.ToInt32(fa.Rows[1]["chiphiKCB"]) + Convert.ToInt32(fa.Rows[14]["chiphiKCB"]);
                fa.Rows[15]["chenhlech"] = Convert.ToInt32(fa.Rows[10]["chenhlech"]) + Convert.ToInt32(fa.Rows[1]["chenhlech"]) + Convert.ToInt32(fa.Rows[14]["chenhlech"]);

                txtThanhToan.Value = Convert.ToInt32(fa.Rows[10]["chiphiKCB"]) + Convert.ToInt32(fa.Rows[1]["chiphiKCB"]) + Convert.ToInt32(fa.Rows[14]["chiphiKCB"]);
                txtBHXHTT.Value = BHXHTT(Convert.ToInt32(txtThanhToan.Value), cboKetLuan.SelectedIndex);
                txtNBChiTra.Value = NBTT();
            }
        }
        public void TongDVKTC()
        {
            fa.Rows[10]["chiphi"] = 0;
            fa.Rows[10]["chiphiKCB"] = 0;
            fa.Rows[10]["chenhlech"] = 0;
            for (int i = 11; i <= 13; i++)
            {
                fa.Rows[10]["chiphi"] = Convert.ToInt32(fa.Rows[10]["chiphi"]) + Convert.ToInt32(fa.Rows[i]["chiphi"]);
                fa.Rows[10]["chiphiKCB"] = Convert.ToInt32(fa.Rows[10]["chiphiKCB"]) + Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
                fa.Rows[10]["chenhlech"] = Convert.ToInt32(fa.Rows[10]["chenhlech"]) + Convert.ToInt32(fa.Rows[i]["chenhlech"]);
            }
        }
        public void TongDVCB()
        {
            fa.Rows[1]["chiphi"] = 0;
            fa.Rows[1]["chiphiKCB"] = 0;
            fa.Rows[1]["chenhlech"] = 0;
            for (int i = 2; i <= 10; i++)
            {
                fa.Rows[1]["chiphi"] = Convert.ToInt32(fa.Rows[1]["chiphi"]) + Convert.ToInt32(fa.Rows[i]["chiphi"]);
                fa.Rows[1]["chiphiKCB"] = Convert.ToInt32(fa.Rows[1]["chiphiKCB"]) + Convert.ToInt32(fa.Rows[i]["chiphiKCB"]);
                fa.Rows[1]["chenhlech"] = Convert.ToInt32(fa.Rows[1]["chenhlech"]) + Convert.ToInt32(fa.Rows[i]["chenhlech"]);
            }

        }
        public int BHXHTT(int KCBThanhToan, int ketluan)
        {
            Int32 Tien = 0;
            int noitrumuc1 = global::TTTTBHYT.Properties.Settings.Default.Noitrumuc1,
            noitrumuc2 = global::TTTTBHYT.Properties.Settings.Default.Noitrumuc2,
            noitrumuc3 = global::TTTTBHYT.Properties.Settings.Default.Noitrumuc3;
            int ngoaitrumuc1 = global::TTTTBHYT.Properties.Settings.Default.ngoaitrumuc1,
                ngoaitrumuc2 = global::TTTTBHYT.Properties.Settings.Default.ngoaitrumuc2,
                ngoaitrumuc3 = global::TTTTBHYT.Properties.Settings.Default.ngoaitrumuc3;
            double tienngay = 1;
            if (cboLoaiDieuTri.SelectedIndex == 1 && txtNgayTT.Text != "" && txtSoNgay.Text != "" && txtNgayTT.Text != "0")
            {
                tienngay = Convert.ToDouble(txtNgayTT.Text) / Convert.ToDouble(txtSoNgay.Text);
            }
            switch (ketluan)
            {
                case 0: //khong dung quy dinh
                    switch (cboTuyen.SelectedIndex)
                    {
                        case 0: // trung uong/ hang 1
                            Tien = Convert.ToInt32(Convert.ToDouble(Convert.ToDouble(KCBThanhToan) * 30 / 100));
                            break;
                        case 1://hang 2
                            Tien = Convert.ToInt32(Convert.ToDouble(Convert.ToDouble(KCBThanhToan) * 50 / 100));
                            break;
                        case 2://hang 3
                            Tien = Convert.ToInt32(Convert.ToDouble(Convert.ToDouble(KCBThanhToan) * 70 / 100));
                            break;
                    }
                    break;
                case 1: //dung quy dinh
                    if (KCBThanhToan <= global::TTTTBHYT.Properties.Settings.Default.mucmin) // so tien thanh toan nho hon muc min
                    {
                        Tien = KCBThanhToan;
                    }
                    else
                    {
                        switch (Convert.ToInt16(txtKCB.Text.Substring(2, 1)))
                        {
                            case 1:
                            case 2: // huong 100
                                Tien = Convert.ToInt32(Convert.ToDouble(KCBThanhToan));
                                break;
                            case 4: // huong 95
                            case 5:
                                Tien = Convert.ToInt32(Convert.ToDouble(Convert.ToDouble(KCBThanhToan) * 95 / 100));
                                break;
                            case 6:
                            case 7: //huong 80
                                Tien = Convert.ToInt32(Convert.ToDouble(Convert.ToDouble(KCBThanhToan) * 80 / 100));
                                break;
                        }
                    }
                    break;
                case 2: //thanh toan tu chon
                    if (cboTuyen.SelectedIndex == 3)
                    {
                        Tien = global::TTTTBHYT.Properties.Settings.Default.tuchonnuocngoai;
                    }
                    else
                    {
                        switch (cboLoaiDieuTri.SelectedIndex)
                        {
                            case 0: //Ngoai tru
                                if (Convert.ToInt32(Convert.ToDouble(KCBThanhToan)) >= ngoaitrumuc3)
                                {
                                    Tien = ngoaitrumuc3; //huong muc 3
                                }
                                else if (Convert.ToInt32(Convert.ToDouble(KCBThanhToan)) >= ngoaitrumuc2)
                                {
                                    Tien = ngoaitrumuc2; // huong muc 2
                                }
                                else if (Convert.ToInt32(Convert.ToDouble(KCBThanhToan)) >= ngoaitrumuc1)
                                {
                                    Tien = ngoaitrumuc1; // huong muc 1
                                }
                                else //huong muc thuc
                                {
                                    switch (Convert.ToInt16(txtKCB.Text.Substring(2, 1)))
                                    {
                                        case 1:
                                        case 2: // huong 100
                                            Tien = Convert.ToInt32(Convert.ToDouble(KCBThanhToan));
                                            break;
                                        case 4: // huong 95
                                            Tien = Convert.ToInt32(Convert.ToDouble(Convert.ToDouble(KCBThanhToan) * 95 / 100));
                                            break;
                                        case 6:
                                        case 7: //huong 80
                                            Tien = Convert.ToInt32(Convert.ToDouble(Convert.ToDouble(KCBThanhToan) * 80 / 100));
                                            break;
                                    }
                                }
                                break;
                            case 1: //noi tru
                                if (Convert.ToInt32(Convert.ToDouble(KCBThanhToan)) >= noitrumuc3)
                                {
                                    Tien = noitrumuc3; //huong muc 3
                                }
                                else if (Convert.ToInt32(Convert.ToDouble(KCBThanhToan)) >= noitrumuc2)
                                {
                                    Tien = noitrumuc2; // huong muc 2
                                }
                                else if (Convert.ToInt32(Convert.ToDouble(KCBThanhToan)) >= noitrumuc1)
                                {
                                    Tien = noitrumuc1; // huong muc 1
                                }
                                else //huong muc thuc
                                {
                                    switch (Convert.ToInt16(txtKCB.Text.Substring(2, 1)))
                                    {
                                        case 1:
                                        case 2: // huong 100
                                            Tien = Convert.ToInt32(Convert.ToDouble(KCBThanhToan));
                                            break;
                                        case 4: // huong 95
                                            Tien = Convert.ToInt32(Convert.ToDouble(Convert.ToDouble(KCBThanhToan) * 95 / 100));
                                            break;
                                        case 6:
                                        case 7: //huong 80
                                            Tien = Convert.ToInt32(Convert.ToDouble(Convert.ToDouble(KCBThanhToan) * 80 / 100));
                                            break;
                                    }
                                }
                                break;

                        }
                    }
                    break;
                case 4: //Nhập tay
                    Tien = Convert.ToInt32(txtBHXHTT.Value);
                    break;
            }
            Tien = Module.Tron100(Convert.ToInt32(Tien * tienngay));
            lblTien.Text = "Bằng Chữ: " + Module.ChangeToCamel(Module.So_chu(Tien).Trim());
            return Tien;
        }
        public int NBTT()
        {
            return Convert.ToInt32(Convert.ToDouble(Convert.ToDouble(txtThanhToan.Value) - Convert.ToDouble(txtBHXHTT.Value)));
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

            Mau29BUS.DeleteRecord(Mau29VO.KeySL);
            Mau29DetailBUS.DeleteRecord(Mau29VO.KeySL);
            ClearControl();
        }

        private void txtMaBV_Leave(object sender, EventArgs e)
        {
            if (txtMaBV.Text.Trim().Length == 5)
            {
                txtTenBV.Text = Mau29BUS.GetTenBV(txtMaBV.Text);
                //SendKeys.Send("{TAB}");
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            DataRow newRow = null;
            Dataset.dsMau29 dsMAU29 = new Dataset.dsMau29();
            newRow = dsMAU29.Tables[0].NewRow();
            newRow["Hoten"] = Mau29VO.HoTen;
            newRow["Ngaysinh"] = Convert.ToDateTime(Mau29VO.NgaySinh).ToString().Substring(0, 10);
            newRow["gioitinh"] = Mau29VO.GioiTinh.ToString();
            if (newRow["gioitinh"].ToString() == "1") { newRow["gioitinh"] = "Nam"; } else { newRow["gioitinh"] = "Nữ"; }
            newRow["Diachi"] = Mau29VO.DiaChi;
            newRow["SoKCB"] = Mau29VO.SoKCB;
            newRow["SOKCB11"] = Mau29VO.SoKCB.Substring(0, 2);
            newRow["SOKCB12"] = Mau29VO.SoKCB.Substring(2, 1);
            if (Mau29VO.SoKCB.Length > 3)
            {
                newRow["SOKCB13"] = Mau29VO.SoKCB.Substring(3, 2);
                newRow["SOKCB14"] = Mau29VO.SoKCB.Substring(5, 2);
                newRow["SOKCB15"] = Mau29VO.SoKCB.Substring(7, 3);
                newRow["SOKCB16"] = Mau29VO.SoKCB.Substring(10);
            }
            newRow["TuNgay"] = Convert.ToDateTime(Mau29VO.TuNgay).ToString().Substring(0, 10);
            newRow["DenNgay"] = Convert.ToDateTime(Mau29VO.DenNgay).ToString().Substring(0, 10);
            newRow["MA_BV"] = Mau29VO.MaBV.Substring(0, 2) + "-" + Mau29VO.MaBV.Substring(2);
            newRow["Ten_BV"] = Mau29VO.TenBV;
            newRow["Ten_BV_DT"] = Mau29VO.TenBVDT;
            newRow["HangBV"] = Mau29VO.HangBV;
            newRow["ChuanDoan"] = Mau29VO.ChuanDoan;
            newRow["Sochungtu"] = Mau29VO.SoChungTu;
            newRow["LoaiDT"] = Mau29VO.LoaiDT;
            
            if (Mau29VO.LoaiDT == 0)
            {
                newRow["TuNgayDT"] = Convert.ToDateTime(Mau29VO.TuNgayDT).ToString().Substring(0, 10);
                
            }
            else
            {
                newRow["SoNgay"] = Mau29VO.SoNgay.ToString();
                newRow["TuNgayDT"] = Convert.ToDateTime(Mau29VO.TuNgayDT).ToString().Substring(0, 10);
                newRow["DenNgayDT"] = Convert.ToDateTime(Mau29VO.DenNgayDT).ToString().Substring(0, 10);
            }

            int i = 2;
            newRow["XetNghiemLyDo"] = Mau29VO.XetNghiemLyDo;
            i++;
            newRow["HaLyDo"] = Mau29VO.HaLyDo;
            i++;
            newRow["ThuocLyDo"] = Mau29VO.ThuocLyDo;
            i++;
            newRow["MauLyDo"] = Mau29VO.MauLyDo;
            i++;
            newRow["ThuthuatLyDo"] = Mau29VO.ThuthuatLyDo;
            i++;
            newRow["VatTuLyDo"] = Mau29VO.VatTuLyDo;
            i++;
            newRow["TienKhamLyDo"] = Mau29VO.TienKhamLyDo;
            i++;
            newRow["KhacLyDo"] = Mau29VO.KhacLyDo;
            i++;
            i++;
            newRow["PhauThuatCaoLyDo"] = Mau29VO.PhauThuatCaoLyDo;
            i++;
            newRow["VatTuCaoLyDo"] = Mau29VO.VatTuCaoLyDo;
            i++;
            newRow["LamSangCaoLyDo"] = Mau29VO.LamSangCaoLyDo;
            i++;
            newRow["ThuocKLyDo"] = Mau29VO.ThuocKLyDo;

            i = 2;
            newRow["XetNghiem"] = string.Format("{0:###,##.###}", Mau29VO.XetNghiem);
            i++;
            newRow["Ha"] = string.Format("{0:###,##.###}", Mau29VO.Ha);
            i++;
            newRow["Thuoc"] = string.Format("{0:###,##.###}", Mau29VO.Thuoc);
            i++;
            newRow["Mau"] = string.Format("{0:###,##.###}", Mau29VO.Mau);
            i++;
            newRow["Thuthuat"] = string.Format("{0:###,##.###}", Mau29VO.Thuthuat);
            i++;
            newRow["VatTu"] = string.Format("{0:###,##.###}", Mau29VO.VatTu);
            i++;
            newRow["TienKham"] = string.Format("{0:###,##.###}", Mau29VO.TienKham);
            i++;
            newRow["Khac"] = string.Format("{0:###,##.###}", Mau29VO.Khac);
            i++;
            i++;
            newRow["PhauThuatCao"] = string.Format("{0:###,##.###}", Mau29VO.PhauThuatCao);
            i++;
            newRow["VatTuCao"] = string.Format("{0:###,##.###}", Mau29VO.VatTuCao);
            i++;
            newRow["LamSangCao"] = string.Format("{0:###,##.###}", Mau29VO.LamSangCao);
            i++;
            newRow["ThuocK"] = string.Format("{0:###,##.###}", Mau29VO.ThuocK);

            i = 2;
            newRow["XetNghiemKCB"] = string.Format("{0:###,##.###}", Mau29VO.XetNghiemKCB);
            i++;
            newRow["HaKCB"] = string.Format("{0:###,##.###}", Mau29VO.HaKCB);
            i++;
            newRow["ThuocKCB"] = string.Format("{0:###,##.###}", Mau29VO.ThuocKCB);
            i++;
            newRow["MauKCB"] = string.Format("{0:###,##.###}", Mau29VO.MauKCB);
            i++;
            newRow["ThuthuatKCB"] = string.Format("{0:###,##.###}", Mau29VO.ThuthuatKCB);
            i++;
            newRow["VatTuKCB"] = string.Format("{0:###,##.###}", Mau29VO.VatTuKCB);
            i++;
            newRow["TienKhamKCB"] = string.Format("{0:###,##.###}", Mau29VO.TienKhamKCB);
            i++;
            newRow["KhacKCB"] = string.Format("{0:###,##.###}", Mau29VO.KhacKCB);
            i++;
            i++;
            newRow["PhauThuatCaoKCB"] = string.Format("{0:###,##.###}", Mau29VO.PhauThuatCaoKCB);
            i++;
            newRow["VatTuCaoKCB"] = string.Format("{0:###,##.###}", Mau29VO.VatTuCaoKCB);
            i++;
            newRow["LamSangCaoKCB"] = string.Format("{0:###,##.###}", Mau29VO.LamSangCaoKCB);
            i++;
            newRow["ThuocKKCB"] = string.Format("{0:###,##.###}", Mau29VO.ThuocKKCB);
            //chenh lech
            i = 2;
            newRow["XetNghiemCL"] = string.Format("{0:###,##.###}", Mau29VO.XetNghiem - Mau29VO.XetNghiemKCB);
            i++;
            newRow["HACL"] = string.Format("{0:###,##.###}", Mau29VO.Ha - Mau29VO.HaKCB);
            i++;
            newRow["ThuocCL"] = string.Format("{0:###,##.###}", Mau29VO.Thuoc - Mau29VO.ThuocKCB);
            i++;
            newRow["MauCL"] = string.Format("{0:###,##.###}", Mau29VO.Mau - Mau29VO.MauKCB);
            i++;
            newRow["ThuThuatCL"] = string.Format("{0:###,##.###}", Mau29VO.Thuthuat - Mau29VO.ThuthuatKCB);
            i++;
            newRow["VattuCL"] = string.Format("{0:###,##.###}", Mau29VO.VatTu - Mau29VO.VatTuKCB);
            i++;
            newRow["TienKhamCL"] = string.Format("{0:###,##.###}", Mau29VO.TienKham - Mau29VO.TienKhamKCB);
            i++;
            newRow["KhacCL"] = string.Format("{0:###,##.###}", Mau29VO.Khac - Mau29VO.KhacKCB);
            i++;
            i++;
            newRow["PhauThuatCaoCL"] = string.Format("{0:###,##.###}", Mau29VO.PhauThuatCao - Mau29VO.PhauThuatCaoKCB);
            i++;
            newRow["VattuCaoCL"] = string.Format("{0:###,##.###}", Mau29VO.VatTuCao - Mau29VO.VatTuCaoKCB);
            i++;
            newRow["LamSangCaoCL"] = string.Format("{0:###,##.###}", Mau29VO.LamSangCao - Mau29VO.LamSangCaoKCB);
            i++;
            newRow["ThuocKCL"] = string.Format("{0:###,##.###}", Mau29VO.ThuocK - Mau29VO.ThuocKKCB);

            newRow["CBTT"] = string.Format("{0:###,##.###}", fa.Rows[1]["chiphi"]);
            newRow["CBBHYT"] = string.Format("{0:###,##.###}", fa.Rows[1]["chiphiKCB"]);
            newRow["CBKTT"] = string.Format("{0:###,##.###}", fa.Rows[1]["chenhlech"]);

            newRow["TCTT"] = string.Format("{0:###,##.###}", fa.Rows[10]["chiphi"]);
            newRow["TCBHYT"] = string.Format("{0:###,##.###}", fa.Rows[10]["chiphiKCB"]);
            newRow["TCKTT"] = string.Format("{0:###,##.###}", fa.Rows[10]["chenhlech"]);


            newRow["BANGCHU"] = lblTien.Text;

            newRow["TTT"] = string.Format("{0:###,##.###}", fa.Rows[15]["chiphi"]);
            newRow["TBHYT"] = string.Format("{0:###,##.###}", fa.Rows[15]["chiphiKCB"]);
            newRow["TKTT"] = string.Format("{0:###,##.###}", fa.Rows[15]["chenhlech"]);


            newRow["TIENTHANHTOAN"] = txtThanhToan.Text;
            newRow["BHXHTHANHTOAN"] = txtBHXHTT.Text;
            newRow["NBTHANHTOAN"] = txtNBChiTra.Text;

            dsMAU29.Tables[0].Rows.Add(newRow);

            if (Mau29VO.KetLuan == (int)Module.Ketluan.Nhaptheongay) //nhap theo ngay
            {
                dsMAU29.Tables[0].Rows[0]["TuNgayDT"] = "";
                for (i = 1; i < fa1.Rows.Count - 2; i++)
                {
                    if (fa1.Rows[i]["diengiai"] != null)
                    {
                        newRow = dsMAU29.Tables[2].NewRow();
                        newRow["KEYGH"] = Mau29VO.KeySL;
                        newRow["Ngay"] = fa1.Rows[i]["diengiai"].ToString();
                        newRow["TienChitra"] = Convert.ToInt32(fa1.Rows[i]["chiphi"]);
                        newRow["TienKCB"] = Convert.ToInt32(fa1.Rows[i]["chiphiKCB"]);
                        newRow["TienCL"] = Convert.ToInt32(fa1.Rows[i]["chenhlech"]);
                        newRow["LyDo"] = Convert.ToString(fa1.Rows[i]["Lydo"]);
                        dsMAU29.Tables[0].Rows[0]["TuNgayDT"] = dsMAU29.Tables[0].Rows[0]["TuNgayDT"].ToString() + newRow["Ngay"].ToString() + "+";
                        if (Convert.ToString(fa1.Rows[i]["Ketluan"]) != "Nhập tay")
                        {
                            newRow["Ketluan"] = Convert.ToString(fa1.Rows[i]["Ketluan"]);
                        }
                        dsMAU29.Tables[2].Rows.Add(newRow);
                    }

                }
                dsMAU29.Tables[0].Rows[0]["TuNgayDT"] = dsMAU29.Tables[0].Rows[0]["TuNgayDT"].ToString().Substring(0, dsMAU29.Tables[0].Rows[0]["TuNgayDT"].ToString().Length - 1);
            }
            Reports f = new Reports();
            ReportDocument cr = new ReportDocument();
            if (Mau29VO.KetLuan == (int)Module.Ketluan.Nhaptheongay) //nhap theo ngay
            {
                cr.Load("rptMau29Detail.rpt");
            }
            else
            {
                cr.Load("rptMau29.rpt");
            }
            cr.SetDataSource(dsMAU29);
            foreach (CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition thisFormulaField in cr.DataDefinition.FormulaFields)
            {
                switch (thisFormulaField.Name)
                {
                    case "Giamdoc":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Giamdoc + "  '";
                        break;
                    case "Giamdinh":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Giamdinh + "  '";
                        break;
                    case "Ketoan":
                        thisFormulaField.Text = "'" + global::TTTTBHYT.Properties.Settings.Default.Ketoantruong + "'";
                        break;

                }
            }
            f.crpView.ReportSource = cr;
            f.Show();
        }

        private void cboKetLuan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboKetLuan.SelectedIndex == 3)
            {
                fa.Visible = false; ;
                fa1.Visible = true;
            }
            else
            {
                fa.Visible = true;
                fa1.Visible = false;
            }
        }
        int GetKetLuan(string strKetLuan)
        {
            switch (strKetLuan)
            {
                case "Không đúng quy định":
                    return 0;
                case "Đúng quy định":
                    return 1;
                case "KCB tự chọn":
                    return 2;
                case "Nhập tay":
                    return 4;
            }
            return 0;
        }
        private void fa1_AfterEdit(object sender, RowColEventArgs e)
        {
            int intBHXHTT = 0;
            if (fa1.Selection.TopRow >= 1)
            {
                fa1.Rows[fa1.Selection.TopRow]["chenhlech"] =
                  Convert.ToInt32(fa1.Rows[fa1.Selection.TopRow]["chiphi"]) -
                   Convert.ToInt32(fa1.Rows[fa1.Selection.TopRow]["chiphiKCB"]);
                fa1.Rows[fa1.Rows.Count - 1]["chiphi"] = 0;
                fa1.Rows[fa1.Rows.Count - 1]["chiphiKCB"] = 0;
                fa1.Rows[fa1.Rows.Count - 1]["chenhlech"] = 0;
                for (int i = 1; i < fa1.Rows.Count - 2; i++)
                {
                    fa1.Rows[fa1.Rows.Count - 1]["chiphi"] = Convert.ToInt32(fa1.Rows[fa1.Rows.Count - 1]["chiphi"]) + Convert.ToInt32(fa1.Rows[i]["chiphi"]);
                    fa1.Rows[fa1.Rows.Count - 1]["chiphiKCB"] = Convert.ToInt32(fa1.Rows[fa1.Rows.Count - 1]["chiphiKCB"]) + Convert.ToInt32(fa1.Rows[i]["chiphiKCB"]);
                    fa1.Rows[fa1.Rows.Count - 1]["chenhlech"] = Convert.ToInt32(fa1.Rows[fa1.Rows.Count - 1]["chenhlech"]) + Convert.ToInt32(fa1.Rows[i]["chenhlech"]);
                    if (fa1.Rows[i]["Ketluan"] != null)
                    {
                        if (GetKetLuan(fa1.Rows[i]["Ketluan"].ToString()) == 4)
                        {
                            intBHXHTT = intBHXHTT + Convert.ToInt32(fa1.Rows[i]["chiphiKCB"]);
                        }
                        else
                        {
                            intBHXHTT = intBHXHTT + BHXHTT(Convert.ToInt32(fa1.Rows[i]["chiphiKCB"]), GetKetLuan(fa1.Rows[i]["Ketluan"].ToString()));
                        }
                    }
                }

                txtThanhToan.Value = fa1.Rows[fa1.Rows.Count - 1]["chiphiKCB"];
                intBHXHTT = Module.Tron100(intBHXHTT);
                txtBHXHTT.Value = intBHXHTT;
                txtNBChiTra.Value = Convert.ToInt32(txtThanhToan.Value) - intBHXHTT;
                lblTien.Text = "Bằng Chữ: " + Module.ChangeToCamel(Module.So_chu(Convert.ToDouble(txtBHXHTT.Value)).Trim());
            }
        }

        private void txtMaBVDT_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F11 && e.Shift == true)
            {
                Cursor.Current = Cursors.WaitCursor;
                frmBV childForm = new frmBV();
                childForm.ShowDialog();
                txtMaBVDT.Text = childForm.Mabv;
                txtTenBVDT.Text = childForm.Tenbv;
                Cursor.Current = Cursors.Default;
                txtTenBVDT.Focus();
            }
        }
        private void txtMaBV_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F11 && e.Shift == true)
            {
                Cursor.Current = Cursors.WaitCursor;
                frmBV childForm = new frmBV();
                childForm.ShowDialog();
                txtMaBV.Text = childForm.Mabv;
                txtTenBV.Text = childForm.Tenbv;
                Cursor.Current = Cursors.Default;
                txtTenBV.Focus();
            }
        }
        private void cboICD_SelectedValueChanged(object sender, EventArgs e)
        {
            txtChanDoan.Text =  Converter.TCVN3ToUnicode(cboICD.Columns[1].Value.ToString()).Trim(); 
        }

        private void txtDenNgayDT_Leave(object sender, EventArgs e)
        {
            if (txtTuNgayDT.Text != "" && txtDenNgayDT.Text != "")
            {
                DateTime t1 = Convert.ToDateTime(txtTuNgayDT.Text);
                DateTime t2 = Convert.ToDateTime(txtDenNgayDT.Text);
                TimeSpan ts;
                ts = t2 - t1;
                txtSoNgay.Text = (ts.TotalDays).ToString();
            }
        }

        private void cboTinhtrang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboTinhtrang.SelectedIndex == 0)
            {
                txtNtt.Value = "";
                txtNtt.Enabled = false;

            }
            else
            {
                txtNtt.Enabled = true;
            }
        }

      

     

    }
}